package priv.conceit.sc.hoxton.file.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * minio配置自动装载
 *
 * @author conceit
 * @since 1.0.0, 2020/9/28
 */
@Configuration
@ConditionalOnClass({MinioClient.class,FileProperties.class,FileSourceProperties.class})
@ConditionalOnProperty(prefix=FileSourceProperties.PREFIX,name = MinioProperties.PREFIX,matchIfMissing=true)
@EnableConfigurationProperties({MinioProperties.class})
public class MinioAutoConfig {

	/**
	 * minioClient 具体的装载bean
	 *
	 * @param prop 装载bean
	 * @return io.minio.MinioClient
	 * @author conceit
	 * @date 2020/9/29 10:23
	 */
	@Bean
	@ConditionalOnMissingBean
	public MinioClient minioClient(MinioProperties prop) throws InvalidPortException, InvalidEndpointException {
		//如果使用定义的port
		if(prop.getPort()>0){
			return new MinioClient(prop.getEndpoint(),prop.getPort(),prop.getAccessKey(),prop.getSecretKey(),prop.isSecure());
		}
		return new MinioClient(prop.getEndpoint(),prop.getAccessKey(),prop.getSecretKey(),prop.isSecure());
	}

}
